#include "PageTurner.h"
#include "ui_PageTurner.h"
#include <QDebug>

CPageTurner::CPageTurner(QWidget *parent)
    : QWidget(parent)
    , m_ui(new Ui::PageTurner)
	, m_pages(0)
	, m_maxShowPages(0)
{
	m_ui->setupUi(this);
	connect(&m_btnGrp, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(slotButtonClicked(QAbstractButton*)));
}

CPageTurner::~CPageTurner()
{
    delete m_ui;
}

void CPageTurner::setPageCount(int pages, int maxShowPages)
{
	m_pages = pages;
	m_maxShowPages = maxShowPages;
	adjustPage(0);
	if (m_btnGrp.buttons().count() > 0)
	{
		slotButtonClicked(m_btnGrp.button(0));
	}
}

void CPageTurner::on_btnHomePage_clicked()
{
	adjustPage(0);
	QList<QAbstractButton*> btns = m_btnGrp.buttons();
	if (btns.count() > 0)
	{
		slotButtonClicked(btns.at(0));
	}
}

void CPageTurner::on_btnLastPage_clicked()
{
	int tmp = m_pages - m_maxShowPages;
	if (tmp < 0)
	{
		tmp = 0;
	}
	adjustPage(tmp, true);
	QList<QAbstractButton*> btns = m_btnGrp.buttons();
	if (btns.count() > 0)
	{
		slotButtonClicked(btns.at(btns.count() - 1));
	}
}

void CPageTurner::on_btnPrePage_clicked()
{
	int preId = m_btnGrp.checkedId() - 1;
	QPushButton* pBtn = dynamic_cast<QPushButton*>(m_btnGrp.button(preId));
	if (pBtn)
	{
		pBtn->setChecked(true);
		slotButtonClicked(pBtn);
	}
	else
	{
		int tempId = preId - m_maxShowPages < 0 ? 0 : preId - m_maxShowPages + 1;
		adjustPage(tempId);
		m_btnGrp.button(preId)->setChecked(true);
		slotButtonClicked(m_btnGrp.button(preId));
	}
}

void CPageTurner::on_btnNextPage_clicked()
{
	int nextId = m_btnGrp.checkedId() + 1;
	QPushButton* pBtn = dynamic_cast<QPushButton*>(m_btnGrp.button(nextId));
	if (pBtn)
	{
		pBtn->setChecked(true);
		slotButtonClicked(pBtn);
	}
	else
	{
		if (nextId < m_pages - 1)
		{
			adjustPage(nextId);
			slotButtonClicked(m_btnGrp.button(nextId));
		}
	}
}

void CPageTurner::adjustPage(int beginPage, bool bLastPageChecked)
{
	QList<QAbstractButton*> btns = m_btnGrp.buttons();
	for (int i=0;i<btns.count();++i)
	{
		QPushButton* pBtn = dynamic_cast<QPushButton*>(btns.at(i));
		m_ui->hLayPages->removeWidget(pBtn);
		delete pBtn;
	}
	int tmp = beginPage + m_maxShowPages;
	int pageCount = tmp > m_pages ? m_pages : tmp;
	for (int i = beginPage; i < pageCount; ++i)
	{
		QPushButton* pBtn = new QPushButton(this);
		pBtn->setCheckable(true);
		m_btnGrp.addButton(pBtn, i);
		pBtn->setText(QString::number(i + 1));
		pBtn->setProperty("page", QString::number(i));
		m_ui->hLayPages->addWidget(pBtn);
		if (i == beginPage && !bLastPageChecked)
		{
			pBtn->setChecked(true);
		}
		if (bLastPageChecked && i == pageCount - 1)
		{
			pBtn->setChecked(true);
		}
	}
}

void CPageTurner::slotButtonClicked(QAbstractButton *pAbsBtn)
{
	int id = m_btnGrp.id(pAbsBtn);
	if (id == m_pages - 1)
	{
		m_ui->btnNextPage->setEnabled(false);
		m_ui->btnLastPage->setEnabled(false);
	}
	else
	{
		m_ui->btnNextPage->setEnabled(true);
		m_ui->btnLastPage->setEnabled(true);
	}

	if (id == 0)
	{
		m_ui->btnHomePage->setEnabled(false);
		m_ui->btnPrePage->setEnabled(false);
	}
	else
	{
		m_ui->btnHomePage->setEnabled(true);
		m_ui->btnPrePage->setEnabled(true);
	}
	qDebug() << "ID:" << id;
	emit sigCurrentPage(id);
}

